package com.sambo.inspection.common.image;

import android.content.Context;
import android.graphics.Bitmap;
import android.media.MediaMetadataRetriever;
import android.net.Uri;
import android.support.annotation.NonNull;
import android.widget.ImageView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.engine.bitmap_recycle.BitmapPool;
import com.bumptech.glide.load.resource.bitmap.BitmapTransformation;
import com.bumptech.glide.request.FutureTarget;
import com.bumptech.glide.request.RequestOptions;
import com.bumptech.glide.request.target.Target;

import java.io.File;
import java.security.MessageDigest;
import java.util.concurrent.ExecutionException;

import static com.bumptech.glide.load.resource.bitmap.VideoDecoder.FRAME_OPTION;

/**
 * 图片加载工具类封装
 */
public class ImageLoader {

      public static final String ANDROID_RESOURCE = "android.resource://";
      public static final String FOREWARD_SLASH = "/";

      private static class ImageLoaderHolder {
            private static final ImageLoader INSTANCE = new ImageLoader();
      }

      private ImageLoader() {
      }

      public static final ImageLoader getInstance() {
            return ImageLoaderHolder.INSTANCE;
      }

      // 直接加载网络图片
      public void displayImage(Context context, String url, ImageView imageView) {
            Glide.with(context).load(url).into(imageView);
      }

      // 直接加载网络图片
      public void displayImage(Context context, String url, int defaultResId, ImageView imageView) {
            //解决URL没变 图片变了问题
//            ObjectKey objectKey = new ObjectKey(updateTime);
            RequestOptions options = new RequestOptions().error(defaultResId);
            Glide.with(context).load(url).apply(options).into(imageView);
      }

      // 加载SD卡图片
      public void displayImage(Context context, File file, ImageView imageView) {
            RequestOptions options = new RequestOptions().centerCrop();
            Glide.with(context).load(file).apply(options).into(imageView);

      }

      // 加载drawable图片
      public void displayImage(Context context, int resId, ImageView imageView) {
            Glide.with(context).load(resId).into(imageView);
      }

//    // 加载drawable图片显示为圆形图片
//    public void displayCricleImage(Context context, int resId, ImageView imageView) {
//        Glide.with(context).load(resourceIdToUri(context, resId))
//                .transform(new GlideCircleTransform(context)).into(imageView);
//    }

//    // 加载网络图片显示为圆形图片
//    public void displayCricleImage(Context context, String url, ImageView imageView) {
//        RequestOptions options = new RequestOptions().transform(new GlideCircleTransform(context));
//        Glide.with(context).load(url)
//                // .centerCrop()//网友反馈，设置此属性可能不起作用,在有些设备上可能会不能显示为圆形。
//                .apply(options).into(imageView);
//    }

//    // 加载SD卡图片显示为圆形图片
//    public void displayCricleImage(Context context, File file, ImageView imageView) {
//        Glide.with(context).load(file)
//                // .centerCrop()
//                .transform(new GlideCircleTransform(context)).into(imageView);
//
//    }

      public static void loadVideoScreenshot(final Context context, String uri, ImageView imageView, long frameTimeMicros) {
            RequestOptions requestOptions = RequestOptions.frameOf(frameTimeMicros);
            requestOptions.set(FRAME_OPTION, MediaMetadataRetriever.OPTION_CLOSEST);
            requestOptions.transform(new BitmapTransformation() {
                  @Override
                  protected Bitmap transform(@NonNull BitmapPool pool, @NonNull Bitmap toTransform, int outWidth, int outHeight) {
                        return toTransform;
                  }
                  @Override
                  public void updateDiskCacheKey(MessageDigest messageDigest) {
                        try {
                              messageDigest.update((context.getPackageName() + "RotateTransform").getBytes("utf-8"));
                        } catch (Exception e) {
                              e.printStackTrace();
                        }
                  }
            });
            Glide.with(context).load(uri).apply(requestOptions).into(imageView);
      }

      // 将资源ID转为Uri
      public Uri resourceIdToUri(Context context, int resourceId) {
            return Uri.parse(ANDROID_RESOURCE + context.getPackageName() + FOREWARD_SLASH + resourceId);
      }

      /**
       * Glide 获得图片缓存路径
       */
      public String getImagePath(Context context, String imgUrl) {
            String path = null;
            FutureTarget<File> future = Glide.with(context)
                    .load(imgUrl)
                    .downloadOnly(Target.SIZE_ORIGINAL, Target.SIZE_ORIGINAL);
            try {
                  File cacheFile = future.get();
                  path = cacheFile.getAbsolutePath();
            } catch (InterruptedException | ExecutionException e) {
                  e.printStackTrace();
            }
            return path;
      }
}
